package com.wlt.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlt.pojo.Admin;
import com.wlt.result.BaseResult;
import com.wlt.service.AdminService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

/**
 * 后台管理员Controller
 */
@RestController
@RequestMapping("/admin")
public class AdminController
{
    @DubboReference
    private AdminService adminService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    /**
     * 新增管理员
     * @param admin 管理员对象
     * @return      执行结果
     */
    @PostMapping ("/add")
    public BaseResult add (@RequestBody Admin admin)
    {
        // 密码加密
        String password = admin.getPassword();
        password = passwordEncoder.encode(password);
        admin.setPassword(password);
        
        adminService.add(admin);
        
        return BaseResult.success(admin);
    }
    
    /**
     * 修改管理
     * @param admin 管理员对象
     * @return      执行结果
     */
    @PutMapping("update")
    public BaseResult update (@RequestBody Admin admin)
    {
        String password = admin.getPassword();
        // 判断是否输入密码
        if (StringUtils.hasText(password))
        {
            // 密码不为空才对密码进行加密
            password = passwordEncoder.encode(password);
            admin.setPassword(password);
            
        }
        adminService.update(admin);
        
        return BaseResult.success(admin);
    }
    
    /**
     * 删除管理员
     * @param aid   管理员id
     * @return      执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long aid)
    {
        adminService.delete(aid);
        
        return BaseResult.success();
    }
    
    /**
     * 根据id查询管理员
     * @param aid   管理员id
     * @return      查询到的管理员
     */
    @GetMapping("/findById")
    public BaseResult<Admin> findById(Long aid)
    {
        Admin byId = adminService.findById(aid);
        
        return BaseResult.success(byId);
    }
    
    /**
     * 分页查询管理员
     * @param page  页码
     * @param size  每页条数
     * @return      查询结果
     */
    @GetMapping ("/search")
    @PreAuthorize("hasAneyAuthority('/admin/search')")
    public BaseResult<Page<Admin>> search (int page, int size)
    {
        Page<Admin> adminPage = adminService.serachPage(page, size);
        
        return BaseResult.success(adminPage);
    }
    
    /**
     * 修改管理员角色
     * @param aid   管理员id
     * @param rids  角色id
     * @return      执行结果
     */
    @PutMapping("/updateRoleToAdmin")
    public BaseResult updateRoleToAdmin (Long aid, Long[] rids)
    {
        adminService.updateRoleToAdmin (aid, rids);
        
        return BaseResult.success();
    }
    
    /**
     * 获取登录的管理员名
     * @return 管理员名
     */
    @GetMapping("/getUsername")
    public BaseResult<String> getUsername()
    {
        // 1.获取会话对象
        SecurityContext context = SecurityContextHolder.getContext();
        // 2.获取认证对象
        Authentication authentication = context.getAuthentication();
        // 3.获取用户登录信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String username = userDetails.getUsername();
        
        return BaseResult.success(username);
    }
}
